package com.ryder.petmatediarybackend.module.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryder.petmatediarybackend.module.auth.entity.AuthBlacklist;
import com.ryder.petmatediarybackend.module.auth.mapper.AuthBlacklistMapper;
import com.ryder.petmatediarybackend.module.auth.service.AuthBlacklistService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述: Token 黑名单表（Redis主存，DB异步落地）(AuthBlacklist)表服务实现类
 * 
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-10-28
 */
@Slf4j
@Service("authBlacklistService")
@RequiredArgsConstructor
public class AuthBlacklistServiceImpl extends ServiceImpl<AuthBlacklistMapper, AuthBlacklist> implements AuthBlacklistService {

    private final AuthBlacklistMapper authBlacklistMapper;

    /**
     * 分批插入黑名单（忽略重复）
     * @param list 黑名单列表
     * @param batchSize 每批数量
     * @return 插入成功的总数量
     */
    public int batchInsertIgnore(List<AuthBlacklist> list, int batchSize) {
        if (list == null || list.isEmpty()) {
            return 0;
        }

        int totalInserted = 0;
        int size = list.size();

        for (int i = 0; i < size; i += batchSize) {
            int end = Math.min(i + batchSize, size);
            List<AuthBlacklist> batch = list.subList(i, end);
            int inserted = authBlacklistMapper.batchInsertIgnore(batch);
            totalInserted += inserted;
            log.debug("批量插入黑名单：第 {}/{} 批，本批 {} 条，成功 {} 条", 
                    (i / batchSize + 1), (size + batchSize - 1) / batchSize, batch.size(), inserted);
        }

        return totalInserted;
    }
}
